﻿using NB.MGJ.Common.MVC;
using NB.MGJ.Model;
using NB.MGJ.Service;
using NB.MGJ.Service.Origins;
using NB.MGJ.Service.Product;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public class OriginsNodeController : BaseController
    {

        private OriginsNodeService _OriginsNodeService;
        private ProductService _ProdcutService;
        private ProductBatchService _ProductBatchService;
        private OriginsTempletService _Origins_TempletService;



        public OriginsNodeController()
        {
            _OriginsNodeService = new OriginsNodeService();
            _ProdcutService = new ProductService();
            _ProductBatchService = new ProductBatchService();
            _Origins_TempletService = new OriginsTempletService();
        }


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddNodeInfo(int batchId, int productId)
        {
            var nodeList = _OriginsNodeService.GetNodeAllByFlowId(batchId, 1);
            var templetNodeList = _OriginsNodeService.GetNodeAllByFlowId(productId, 0);

            ViewBag.nodeList = nodeList;
            ViewBag.templetNodeList = templetNodeList;
            ViewBag.batchId = batchId;
            ViewBag.CurrentUser = LogOnUser.UserID;
            ViewBag.productId = productId;

            return View();
        }



        public ActionResult AddNodeSelect(int batchId)
        {
            var batchModel = _ProductBatchService.GetModelById(batchId);


            var nodeList = _OriginsNodeService.GetNodeAllByFlowId(batchId, 1);
            var templetNodeList = _OriginsNodeService.GetNodeAllByFlowId(batchId, 0);

            ViewBag.nodeList = nodeList;
            ViewBag.templetNodeList = templetNodeList;
            ViewBag.batchId = batchId;
            ViewBag.CurrentUser = LogOnUser.UserID;
            

            return View();
        }



        public ActionResult AEditNodeInfo(string type, int nodeId, int flowId)
        {
            var nodeModel = _OriginsNodeService.GetModel(nodeId);
            ViewBag.nodeModel = nodeModel;
            ViewBag.flowId = flowId;
            return View();
        }

        /// <summary>
        /// 流程节点保存【也仅限于流程节点保存】
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult NodeSave(Origins_Node model)
        {
            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.ViewDel = 0;
            model.Category = 0;
            model.NodeType = 1;
            int result = 0;
            if (model.Id == 0)
            {
                result = _OriginsNodeService.Add(model);
                model.Id=result;
            }
            else
            {
                result = _OriginsNodeService.Update(model);
            }

            Hashtable ht = new Hashtable();
            ht.Add("model", model);
            ht.Add("result", result);
            return Json(ht);
        }


        public ActionResult _NodeSave(Origins_Node model)
        {
            model.ChangeDate = DateTime.Now;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.ViewDel = 0;//删除状态新建默认为未删除，删除操作调用另一个action


            int result = 0;
            if (model.NodeName == null && model.Id == 0)
            {
                return null; 
            }
            else  if (model.Id == 0)
            {
                result = _OriginsNodeService.Add(model);
            }
            else
            {
                result = _OriginsNodeService.Update(model);
            }

            Hashtable ht = new Hashtable();
            ht.Add("model", model);
            ht.Add("result", result);
            return Json(ht);
        }


        public ActionResult FromTemplet()
        {
            ViewBag.eid = LogOnUser.EnterpriseId;
            return View();
        }



        /// <summary>
        ///   复制模板的节点到当前编辑的模板
        /// </summary>
        /// <param name="sysTempletId">被拷贝模板对象</param>
        /// <param name="templetId">拷贝模板对象</param>
        /// <returns></returns>
        public ActionResult SetTemplet(int sysTempletId, int templetId)
        {
            try
            {

                var sysTempletNodeList = _OriginsNodeService.GetNodeAllByFlowId(sysTempletId, 0);
                //拷贝前先清理原有节点
                _OriginsNodeService.Del_FlowID(templetId, 0);

                foreach (var item in sysTempletNodeList)
                {
                    item.FlowID = templetId;
                    item.ChangeLoginName = LogOnUser.LoginName;
                    item.ChangeDate = DateTime.Now;
                    item.ChangeLoginID = LogOnUser.UserID;
                    item.NodeType = 0;
                    var result = _OriginsNodeService.Add(item);
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
            return Json("1");
        }



        public ActionResult NodeDelete(int nodeId)
        {
            var result = _OriginsNodeService.Del(nodeId);
            return Json(result);
        }

        public ActionResult GetTempletNodeList( int batchId,int productId=0)
        {

            var batchModel = _ProductBatchService.GetModelById(batchId);

            var templetNodeList = _OriginsNodeService.GetNodeAllByFlowId(batchModel.ProductId, 0);
            var flowNodeList = _OriginsNodeService.GetNodeAllByFlowId(batchId, 1);

            foreach (var item in templetNodeList)
            {
                item.ExtEnterTimes = 0;//初始录入次数为0
                item.EXTIsEnter = 0;//默认为0为有权限
                item.EXTIsPermission = 0;//初始为0，有权限

                //获取该模板节点 当前已录入次数
                foreach (var inneritem in flowNodeList)
                
                {
                    if (item.NodeName == inneritem.NodeName)
                    {
                        item.ExtEnterTimes++;
                    }

                }

                if (item.EnterTimes==1)
                {
                    item.EXTIsEnter = 0;
                }
                else if (item.EnterTimes == 0 && item.ExtEnterTimes > 0)
                {
                    item.EXTIsEnter = 1;
                }
                //判断当前用户是否有该节点录入权限
                if (item.Permission == 1)
                {
                    if (item.PermissionPerson == LogOnUser.UserID)
                    {
                        item.EXTIsPermission = 0;
                    }
                    else
                    {
                        item.EXTIsPermission = 1;
                    }
                }


            }

            return Json(templetNodeList);
        }


        public ActionResult GetTempletNodeListContainPermission(int batchId)
        {
            var batchModel = _ProductBatchService.GetModelById(batchId);

            var templetNodeList = _OriginsNodeService.GetNodeAllByFlowId(Convert.ToInt32(batchModel.TempletId), 0);
            var flowNodeList = _OriginsNodeService.GetNodeAllByFlowId(batchId, 1);

            foreach (var item in templetNodeList)
            {
                item.ExtEnterTimes = 0;//初始录入次数为0
                item.EXTIsEnter = 0;//默认为0为有权限
                item.EXTIsPermission = 0;//初始为0，有权限

                //获取该模板节点 当前已录入次数
                foreach (var inneritem in flowNodeList)
                {
                    if (item.NodeName == inneritem.NodeName)
                    {
                        item.ExtEnterTimes++;
                    }

                }

                if (item.EnterTimes == 1)
                {
                    item.EXTIsEnter = 0;
                }
                else if (item.EnterTimes == 0 && item.ExtEnterTimes > 0)
                {
                    item.EXTIsEnter = 1;
                }
                //判断当前用户是否有该节点录入权限
                if (item.Permission == 1)
                {
                    if (item.PermissionPerson == LogOnUser.UserID)
                    {
                        item.EXTIsPermission = 0;
                    }
                    else
                    {
                        item.EXTIsPermission = 1;
                    }
                }


            }

            return Json(templetNodeList);
        }



        public ActionResult GetNodeModel(int nodeId)
        {
            var result = _OriginsNodeService.GetModel(nodeId);
            return Json(result);
        }


        public ActionResult NodeConfig(int batchId)
        {
        var batchModel = _ProductBatchService.GetModelById(batchId);
      
           
            if (string.IsNullOrEmpty(batchModel.TempletId))
            {
                

                var templetModel=new Origins_Templet(){
                    Name=batchModel.BatchName+"初始模板",
                    ChangeDate=DateTime.Now,
                    ChangeLoginID=LogOnUser.UserID,
                    ChangeLoginName=LogOnUser.RealName,
                    EnterpriseId=LogOnUser.EnterpriseId,
                    ViewDel=0,
                    Remark="Because there is no, so new "
                };
                var templetId=_Origins_TempletService.Insert(templetModel);
                batchModel.TempletId = templetId.ToString();
                var result = _ProductBatchService.Updbatch(batchModel);
            }
            ViewBag.TempletId = batchModel.TempletId;
            return View();
        }


        /// <summary>
        /// 获取模板节点列表
        /// </summary>
        /// <param name="templetId">模板ID</param>
        /// <returns>返回列表对象不包含权限信息</returns>
        public ActionResult _GetTempletNodeList(int templetId)
        {
            var templetNodeList = _OriginsNodeService.GetNodeAllByFlowId(templetId, 0);
            return Json(templetNodeList);
        }

        


    }
}